package com.elvende.web.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.elvende.web.client.dto.AffortableHousing;
import com.elvende.web.client.service.LifeService;
import com.elvende.web.client.service.life.HousingService;
import com.elvende.web.server.jdo.PMF;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class HousingServiceImpl extends RemoteServiceServlet implements
	HousingService {
	PersistenceManager pm = PMF.get().getPersistenceManager();
		
	public AffortableHousing addAffortableHousing(String name, String address, String phone){  	  	
	    // AffortableHousing e = new AffortableHousing("Marien-Heim of Sunset Park", "4520 4th Ave,Brooklyn,NY,11220", "718-492-6833");
	  	AffortableHousing e = new AffortableHousing(
	  			name, address, phone);  	
	    try {
	        pm.makePersistent(e);
	    } finally {
	        pm.close();
	    }
	    return e;
	}

	public AffortableHousing[] listAffortableHousing(){
		Query query = pm.newQuery(AffortableHousing.class);
		// Query query = pm.newQuery("select from AffortableHousing");
		AffortableHousing[] list = null;
		 try {			 	
		        List<AffortableHousing> results = (List<AffortableHousing>) query.execute();
		        list = new AffortableHousing[results.size()];
		        for (int i=0; i<results.size(); i++){
		        	list[i] = (AffortableHousing) results.get(i);	
		        }
		        // list = (AffortableHousing[])results.toArray(new AffortableHousing[]{});
		        
		    } finally {
		        query.closeAll();
		 }		
		return list;
	}

	
}
